import 'package:demo/util/asset.dart';
import 'package:flutter/material.dart';
import 'dart:convert';

class BannerCom extends StatefulWidget {
  @override
  _BannerComState createState() => _BannerComState();
}

class _BannerComState extends State<BannerCom> {
  var myData, sumSpend, sumIncome;
  var month = DateTime.now().month;
  var year = DateTime.now().year;

  void initData() {
    myData = jsonDecode(SpUtil.preferences.getString('myData'));
    print('````````bannerObject````````');
    print(myData);

    for (var item in myData) {
      if (year == item['year']) {
        for (var monthItem in item['monthList']) {
          if (month == monthItem['month']) {
            sumSpend = monthItem['sumSpend'];
            sumIncome = monthItem['sumIncome'];
          }
        }
      }
    }
  }

  Row textRow(String text, double fontSize, bool weight) {
    return Row(
      children: [
        new Text(
          text,
          style: TextStyle(
            fontSize: fontSize,
            fontWeight: weight ? FontWeight.bold : FontWeight.normal,
            color: Colors.white,
            letterSpacing: 0.5,
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    initData();
    return Container(
      padding: const EdgeInsets.all(20.0),
      height: 240.0,
      alignment: Alignment.bottomLeft,
      decoration: new BoxDecoration(
        image: DecorationImage(
          image: AssetImage('images/city.jpeg'),
          fit: BoxFit.cover,
        ),
      ),
      child: Column(mainAxisAlignment: MainAxisAlignment.end, children: [
        textRow('$month月·支出', 20.0, false),
        SizedBox(height: 10),
        textRow('$sumSpend元', 36.0, true),
        SizedBox(height: 10),
        textRow('本月收入：$sumIncome元', 16.0, false),
      ]),
    );
  }
}
